/*
 * WriteComments.fx
 * Author:Yu Min-ting
 * Created on 2009-7-20, 14:29:54
 */

package com.sjtu.fx.comments;

import javafx.scene.*;
import com.sjtu.fx.tools.Panel;
import javafx.scene.control.Label;

import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import javafx.scene.control.Button;


import com.sjtu.fx.tools.SwingTextArea;


import com.sjtu.fx.book.BookDetails;
import com.sjtu.fx.user.Login;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONObject;
import javafx.scene.control.RadioButton;

import javafx.scene.control.ToggleGroup;

import com.sjtu.fx.tools.UIAlert;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;


public class WriteComments extends CustomNode{
     var panel:Panel;
     public var cScore:Number;
     public var aScore:Double on replace with{
         if(aScore<=5 and aScore>=4.5)
            urls = "{__DIR__}star5.png";
         if(aScore<4.5 and aScore>=3.5)
            urls = "{__DIR__}star4.png";
         if(aScore<3.5 and aScore>=2.5)
            urls = "{__DIR__}star3.png";
         if(aScore<2.5 and aScore>=1.5)
            urls = "{__DIR__}star2.png";
         if(aScore<1.5 and aScore>=0)
            urls = "{__DIR__}star1.png"
     };
     var urls:String = bind urls with inverse;
     

     var inform = Label {
             text: "A high score indicates that you like it"
             font: Font {size:12 name: "Verdana" }
             textFill: Color.rgb(11,51,60)
         }

      var group = ToggleGroup{};

      var one = RadioButton {
            text: "1"
            toggleGroup: group
            selected:true
      }
      var two = RadioButton {
            text: "2"
            toggleGroup: group
      }
      var three = RadioButton {
            text: "3"
            toggleGroup: group
      }
       var four = RadioButton {
            text: "4"
            toggleGroup: group
      }
       var five = RadioButton {
            text: "5"
            toggleGroup: group
      }


     var score = Label {
             text: "Score"
             font: Font {size:12 name: "Verdana Bold"}
             textFill: Color.rgb(11,51,60)
         }
    var avgScore = Label {
             text: "Average Score: {aScore}"
             font: Font {size:12 name: "Verdana Bold"}
             textFill: Color.rgb(11,51,60)
         }
    var star = Image {
          url: {urls}
    }



    var commentInform = Label {
             text: "Comment on Book:"
             font: Font {size:12 name: "Verdana" }
             textFill: Color.rgb(11,51,60)
         }
    var comment = SwingTextArea{
        columns:66
        rows:5
        editable:true
    }
    var similarInform = Label {
             text: "You can refer people to read some similar books about this topic:"
             font: Font {size:12 name: "Verdana" }
             textFill: Color.rgb(11,51,60)
         }
    var similar = SwingTextArea{
        columns:66
        rows:3
        editable:true
    }

     var alert = UIAlert {
         translateX:85
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:260
         height:120
         visible:false
         opacity:0.9
     }
     
     var button = Button {
             translateX:335
             text: "Comment"
             action: function() {
                 var userId:Integer = (parent.parent.parent.lookup("loginPanel") as Login).userid;
                 var bookId:Integer = (parent.parent.parent.lookup("borrowBook") as BookDetails).bookId;
                 if(userId==0)
                 {
                     alert.title = "Blank User";
                     alert.content ="Login First";
                     alert.visible = true;
                     //Alert.inform("Blank User","Login First");
                 }else{
                 addBookComment(userId,bookId,comment.text,Integer.parseInt(group.selectedButton.text));
                 }
             }
         }

     override protected function create() : Node{
          Group{
            content:[
                panel = Panel {
                    width:    780
                    height:   305
                    title:    "Write Comments"
                },
                VBox{
                    spacing:6
                    translateX:25 translateY:50
                    content:[
                            HBox{
                                spacing: 20
                                content:[inform,score,one,two,three,four,five,avgScore,
                                ImageView{
                                    image:bind star
                                    fitHeight: 20
                                    preserveRatio: true
                                    cache: true
                                    smooth: true
                                }]
                            },
                            commentInform, comment,
                            similarInform, similar,
                            button

                        ]
                },alert
                
                ]
          }
     }

function addBookComment(userId:Integer,bookId:Integer,comment:String,score:Integer){
     HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/AddComment"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("userId",userId);
                json.put("bookId", bookId);
                json.put("comment",comment);
                json.put("score",score);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                alert.title ="Result";
                alert.content = result;
                alert.visible = true;
                //Alert.inform(result);
            } finally {
                is.close();
            }
        }
    }.start();
}

}
